package com.study.controller;

import com.study.annotation.NotControllerResponseAdvice;
import com.study.enums.ResultCodeEnum;
import com.study.pojo.common.Result;
import com.study.pojo.dto.Award;
import com.study.pojo.dto.Heartbeat;
import com.study.service.AwardService;
import com.study.service.HeartbeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/award")
public class AwardController {
    @Autowired
    private AwardService awardService;
    @Autowired
    private HeartbeatService heartbeatService;

    @PostMapping("/insert")
    @NotControllerResponseAdvice
    public Result<?> insertAward(@Validated @RequestBody Award award) {
        awardService.insertAward(award);
        Award lastAward = awardService.getLastAward();
        return Result.success(ResultCodeEnum.SAVE_SUCCESS, lastAward);
    }


    // 获取奖品列表 按照添加id倒序
    @RequestMapping("/userid/{id}")
    public List<Award> getAwardList(@PathVariable("id") Integer id) {

        return awardService.queryAwardByUserId(id);
    }

    /**
     * 改变奖品状态
     *
     * @param id     奖品id
     * @param status 兑换状态(‘0’未领取 ‘1’已领取未发奖 '2'已发奖 ‘3’已使用)
     */
    @RequestMapping("/change/{id}/{status}")
    public Result<?> changeStatus(@PathVariable("id") Integer id, @PathVariable("status") String status) {
        int i = awardService.changeStatus(id, status);

        return Result.success(ResultCodeEnum.CHANGE_SUCCESS);
    }

    /**
     * 兑换奖品
     *
     * @param meId  我的id
     * @param price 奖品价格
     */

    @RequestMapping("/get/award")
    public Result<?> getAward(Integer meId, Integer awardId, Integer price) {
        awardService.getAward(meId, awardId, price);
        return Result.success(ResultCodeEnum.RECEIVE_SUCCESSFULLY);
    }

    @RequestMapping("/delete/{id}")
    public Result<?> deleteAward(@PathVariable("id") Integer id) {
        int i = awardService.deleteAwardById(id);

        return Result.success(ResultCodeEnum.DELETE_SUCCESS);
    }
}
